'use strict'
var Wechat = require('./wechat')
var config = require('./config')
var db = require('./db')
var wechatApi = new Wechat(config.wechat)
wechatApi.selfMenu();
exports.reply = function *(next){
   //console.log(message)
    var message = this.weixin
    if (message.MsgType === 'event') {
        if (message.Event === 'subscribe') {
            if (message.EventKey) {
                console.log('扫码进入');
            }
            this.body = '你关注了公众号'
        }else if (message.Event === 'unsubscribe') {
            console.log('取关');
            this.body = ''
        }
        else if (message.Event === 'LOCATION') {
             
             this.body = '地理位置纬度: '+message.Latitude+'地理位置经度: '+message.Longitude+'地理位置精度: '+message.Precision
        }
        else if (message.Event === 'location_select') {
             
            this.body = '地理位置纬度: '+message.SendLocationInfo.Location_X+'地理位置经度: '+message.SendLocationInfo.Location_Y+'地理位置精度: '+message.SendLocationInfo.Label
       }
        else if (message.Event === 'CLICK') {
            if(message.EventKey==='V1001_GOOD'){
                this.body = '感谢你的赞'  
            } else  this.body = '你点击了菜单'+message.EventKey   
       }
       else if (message.Event === 'SCAN') {
            console.log('关注后扫二维码'+message.EventKey+' '+ message.Ticket  )
        this.body = '看到你扫一下'   
        }
        else if (message.Event === 'scancode_waitmsg') {
            console.log('关注后扫二维码'+message.EventKey+' '+ message.ScanCodeInfo.ScanResult  )
            this.body = ''   
        }
        else if (message.Event === 'scancode_push') {
             
        //this.body = message.ScanCodeInfo.ScanResult   
        }
       else if (message.Event === 'VIEW') {
            
            this.body = '你点击了菜单中的链接'+message.EventKey   
        }
     }
    else if (message.MsgType === 'text') {
        var content = message.Content;
        var reply = '嘿嘿'
        if (content === '1') {
            reply = 'hello'
        }else if (content === '2') {
           reply =  [
            {
                Title: '张璀测试',
                Description: '图文消息描述',
                PicUrl: 'http://f10.baidu.com/it/u=3038573891,4200009349&fm=72',
                Url: 'http://www.baidu.com'
            },
            {
                Title: '张璀测试',
                Description: '图文消息描述',
                PicUrl: 'http://f10.baidu.com/it/u=3038573891,4200009349&fm=72',
                Url: 'http://www.baidu.com'
            }
        ]
        }else if (content === '3') {
            reply = 'http://text.tunnel.qydev.com/dist/'
        }
        else if (content === '4') {
            var data = yield wechatApi.uploadTempMaterial('image',__dirname+'/2.png') 
           reply = {
               type:'image',
               MediaId:data.media_id
           } 
        }
        else if (content === '10') {
           var data = yield wechatApi.uploadPermMaterial('pic',__dirname+'/2.png')
           reply =  ''
        }
        else if (content === '11') {
            var data = yield wechatApi.batchgetMaterial('image',0,10)
            console.log(data)
            reply =  JSON.stringify(data)
         }
        else if (content === '创建分组') {
          
            var data = yield wechatApi.createGroup('朋友').then(function(data){
              return data;
            });
            reply = JSON.stringify(data)
         
        }
        else if (content === '获取分组') {
           var data =  yield wechatApi.getGroups('朋友')
            .then(function(data){
                    return data;
                   
                })
            reply = JSON.stringify(data)
        }
        else if (content === '删除分组') {
            yield wechatApi.deleteGroups([107])
            
             reply = '删除分组成功'
         }
         else if (content === '5') {
            yield wechatApi.updateUserRemark('opMq3w7uy8lxkuiTrVtF766Clk0Y','lch')

            reply = '你的备注名为：lch'
         }
         else if(content === '获取用户列表'){
             
            var data = yield wechatApi.getUserList()
            
            reply = JSON.stringify(data)
         }else if(content === '6'){
            var data= yield db.connect() 
            reply = JSON.stringify(data)
         }
         else if(content ==='7'){
            var text = {
                content:'这是群发消息测试唔~'
            };
           var msg =  yield wechatApi.massSendMsg('text',text)

            
         }
        this.body = reply
    }  
    yield next
}   

